package 作业;

import java.util.Date;
import java.util.HashMap;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

class manage{
	public static String path="info.txt";
	public static userinformation2 use;
	public static userinformation2 getuse() throws IOException, ClassNotFoundException {
		File infofile=new File(path);
		System.out.println(infofile.exists()+infofile.getAbsolutePath());
		if(!infofile.exists()||infofile.length()==0) {
			infofile.createNewFile();
			use=new userinformation2();
		}
		else{
			InputStream i=new FileInputStream(infofile);
			ObjectInputStream oi=new ObjectInputStream(i);
			info temp=(info) oi.readObject();
			use=new userinformation2(temp);
			i.close();
			oi.close();
		}
		return use;
		
	}
}

public class userinformation2 implements Serializable{

    static final long serialVersionUID =1L;//类序列化时设定
	public String name;
	public Set<String> ticket;//当前用户票
	public String password;
	public String zuce;
	public boolean type;//true就是性别
	private File f=new File("info.txt");
	private info t;
	private ObjectOutputStream  o;
	public userinformation2() throws FileNotFoundException, IOException {
		t=new info();
		System.out.println(f.exists()+f.getAbsolutePath());
		o=new ObjectOutputStream(new FileOutputStream(f));
		o.write(11);
		ticket=new HashSet<String>();
	}
	
	public userinformation2(info i) {
		this.t=i;
	}
	
	public boolean add(String t) throws IOException {//添加作用
		DateFormat df=new SimpleDateFormat("YYYY年MM月dd日HH时mm分");
		t=t+df.format(new Date(System.currentTimeMillis()));
		ticket.add(t);
		o.writeObject(t);
		return true;
	}
	
	public boolean del(String t) throws IOException {//删除操作用
		boolean flag;
		flag=ticket.remove(t);
		if(flag) {
			this.t.personticket.put(name,ticket);
			o.writeObject(this.t);
			}
		return flag;
	}
	
	public String[] geth() {//获取历史记录
		String top="发车时间                   起点站              终点站          价格         购买时间";
		String [] ans=new String[ticket.size()+1];
		ans[0]=top;
		String [] temp=ticket.toArray(new String[ticket.size()]);
		for(int i=0;i<ticket.size();i++) {
			ans[i+1]=temp[i];
		}
		return ans;
	}
	public boolean check(String name,String password) {
		System.out.println(t.maps);
		if(t.maps.containsKey(name)) {
			if(t.maps.get(name).equals(password)) {
				this.name=name;
				this.password=password;
				this.ticket=t.personticket.get(name);
				return true;
			}
		}
		return false;
	}
	
	public boolean newuser(String name,String password) throws IOException {
		if(t.maps.containsKey(name)) {
			return false;
		}
		else {
			t.maps.put(name,password);
			this.password=password;
			this.name=name;
			this.t.personticket.put(name, (new HashSet<String>()));
			o.writeObject(t);
			return true;
		}
	}
	public void edpassword(String pass) throws IOException {
		this.t.maps.put(this.name, pass);
		o.writeObject(this.t);
	}
}
class info implements Serializable{
	public Map<String,String> maps;//用户名和密码对应
	public Map<String,Set<String>> personticket;//用户对应的车票记录集合
	public info() {
		maps=new HashMap<String,String>();
		personticket=new HashMap<String,Set<String>>();
	}
}
